/*
Purpose: calculate median income at age 13 by state/year for families with children
from CPS data
*/

import delimited "data/raw/USACPIALLAINMEI",clear
gen year=real(substr(date,1,4))
sort year
gen inflator=usacpiallainmei[64]/usacpiallainmei
keep inflator year
tempfile inflator
save `inflator', replace

use "data/clean/medhhinc_by_state_80_23", clear

merge m:1 year using `inflator'
drop if _merge==2 //unused years
drop _merge

ren hhincome med_income
ren statefip fips_birth
replace med_income = med_income * inflator
drop inflator

gen yob = year - 13
tsset fips_birth yob

gen lnmed_income_age13_kid = ln(med_income)

// 3-year smoothing
gen med_income_smoothed = (med_income + L.med_income + F.med_income) / 3
gen lnmed_income_age13_kid_sm = ln(med_income_smoothed)

keep yob fips_birth lnmed_income_age13_kid_sm

save "data/clean/med_income_age13_child", replace

